<template>
  <div class="rule-container">
    <div class="create-container">
      <el-button type="primary" @click="open">增加停车计费规则</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table :data="ruleList" style="width: 100%">
        <el-table-column type="index" label="序号" />
        <el-table-column label="计费规则编号" prop="ruleNumber" />
        <el-table-column label="计费规则名称" prop="ruleName" />
        <el-table-column label="免费时长(分钟)" prop="freeDuration" />
        <el-table-column label="收费上线(元)" prop="chargeCeiling" />
        <el-table-column label="计费方式" :formatter="fn" prop="chargeType">
          <!-- <template #default="scope" >
            {{ scope.row.chargeType }}
          </template> -->
        </el-table-column>
        <el-table-column label="计费规则" prop="ruleNameView"/>
        <el-table-column label="操作" fixed="right" width="120">
          <template #default="scope">
            <el-button size="mini" type="text" @click="edit(scope.row.id)">编辑</el-button>
            <el-button size="mini" type="text" @click="del(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <div class="page-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="params.page"
        :page-sizes="[2,4,6,8,10]"
        :page-size="params.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>

    <!-- 弹框 -->
    <el-dialog :visible="dialogVisible" width="680px" :title="addForm.id?'编辑规则':'新增规则'" @close="handleClose">
      <!-- 表单接口 -->
      <div class="form-container">
        <el-form ref="addForm" :inline="true" :model="addForm" :rules="addFormRules" label-position="top">
          <el-form-item label="计费规则编号" prop="ruleNumber">
            <el-input v-model="addForm.ruleNumber" />
          </el-form-item>
          <el-form-item label="计费规则名称" prop="ruleName">
            <el-input v-model="addForm.ruleName" />
          </el-form-item>
          <el-form-item label="计费方式" prop="chargeType">
            <el-radio-group v-model="addForm.chargeType" size="small">
              <el-radio label="duration" border>时长收费</el-radio>
              <el-radio label="turn" border>按次收费</el-radio>
              <el-radio label="partition" border>分段收费</el-radio>
            </el-radio-group>
          </el-form-item>
          <div class="flex-container">
            <el-form-item label="免费时长">
              <el-input v-model="addForm.freeDuration" />
            </el-form-item>
            <el-form-item label="收费上限">
              <el-input v-model="addForm.chargeCeiling" />
            </el-form-item>
          </div>
          <el-form-item label="计费规则">
            <!-- 按时长收费区域 -->
            <div v-if="addForm.chargeType==='duration'" class="duration">
                每 <el-input v-model="addForm.durationTime" class="input-box" />
                      <el-select v-model="addForm.durationType">
                        <el-option 
                            v-for="item in durType" 
                            :key="item.value" 
                            :label="item.label" 
                            :value="item.value">
                        </el-option>
                      </el-select>
                <el-input v-model="addForm.durationPrice" class="input-box" /> 元
            </div>
            <!-- 按次收费区域 -->
            <div v-if="addForm.chargeType==='turn'" class="turn">
                每次 <el-input v-model="addForm.turnPrice" class="input-box" /> 元
              </div>
            <!-- 按分段收费区域 -->
            <div v-if="addForm.chargeType==='partition'" class="partition">
              <div class="item">
                  <el-input v-model="addForm.partitionFrameTime" class="input-box" />小时内,每小时收费
                  <el-input v-model="addForm.partitionFramePrice" class="input-box" /> 元
              </div>
              <div class="item">
                  每增加<el-input v-model="addForm.partitionIncreaseTime" class="input-box" />小时,增加
                  <el-input v-model="addForm.partitionIncreasePrice" class="input-box" /> 元
              </div>
            </div>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <el-button size="mini" @click="handleClose">取 消</el-button>
        <el-button size="mini" type="primary" @click="confirm">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {getCarRuleListAPI,addCarRuleListAPI, editCarRuleListAPI,delCarRuleListAPI,getCarRuleDetailAPI} from '@/api/rule'

export default {
  name: 'Building',
  data() {
    return {
      ruleList: [],
      params: {
        page: 1,
        pageSize: 10,
        ruleName:'',
      },
      total: 0,
      dialogVisible: false,
      addForm:{
        ruleNumber: "",
        ruleName: "",
        freeDuration: "",
        chargeCeiling: null,
        chargeType: "",

        durationTime: null,
        durationPrice: null,
        durationType: "",

        turnPrice: null,

        partitionFrameTime: null,
        partitionFramePrice: null,
        partitionIncreaseTime: null,
        partitionIncreasePrice: null
      },
      addFormRules:{
        ruleNumber:[{required:true,message:'计费规则编号不能为空',trigger:'blur'}],
        ruleName:[{required:true,message:'计费规则名称不能为空',trigger:'blur'}],
        chargeType:[{required:true,message:'计费方式不能为空',trigger:'blur'}],
      },
      durType:[
        {value: 'minute' ,label:'分钟'},
        {value: 'hour',label:'小时'},
      ],
    }
  },
  mounted(){
    this.getCarRuleList();
  },
  methods:{
    async getCarRuleList(){
      const res=await getCarRuleListAPI(this.params);
      console.log(res);
      this.ruleList=res.data.rows;
      this.total=res.data.total;
    },
    async edit(id){
      this.open();
      const res=await getCarRuleDetailAPI(id);
      this.addForm=res.data;
      this.addForm.ruleNameView=res.data.ruleNameView;
    },
    del(id){
      this.$confirm('是否确认删除').then(()=>{
        delCarRuleListAPI(id).then(()=>{
          this.$message.success('删除成功');
          this.getCarRuleList();
        }).catch(()=>{
          this.$message.error('删除失败');
          this.getCarRuleList();
        }).catch(()=>{
        })
      })
    },
    confirm(){
      this.$refs.addForm.validate((ok)=>{
        if(ok){
          console.log('检查成功');
          if(this.addForm.id){
            console.log('id已存在,进入编辑模式');
            editCarRuleListAPI(this.addForm);
            this.handleClose();
            this.$message.success('编辑成功');
          }else{
            console.log('id不存在,进入添加模式');
            addCarRuleListAPI(this.addForm);
            this.handleClose();
            this.$message.success('添加成功');
          }
        }
      })
    },
    clearForm(){
      this.addForm={
        ruleNumber: "",
        ruleName: "",
        freeDuration: "",
        chargeCeiling: null,
        chargeType: "",
        durationTime: null,
        durationPrice: null,
        durationType: "",
        turnPrice: null,
        partitionFrameTime: null,
        partitionFramePrice: null,
        partitionIncreaseTime: null,
        partitionIncreasePrice: null
      }
    },
    fn(row){
      if(row.chargeType=='duration'){return '时长收费' }
      if(row.chargeType=='turn'){return '按次收费'}
      if(row.chargeType=='partition'){return '时段收费'}
    },

    open(){
      this.dialogVisible=true;
    },
    handleClose(){
      this.clearForm();
      this.dialogVisible=false;
      this.getCarRuleList();
    },

    handleSizeChange(pageSize){
      this.params.pageSize=pageSize;
      this.getCarRuleList();
    },
    handleCurrentChange(page){
      this.params.page=page;
      this.getCarRuleList();
    },
  },
}
</script>

<style lang="scss" scoped>
.rule-container {
  padding: 20px;
  background-color: #fff;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  padding-bottom: 20px;

  .search-label {
    width: 123px;
  }

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}
.create-container{
  margin: 10px 0px;
}
.page-container{
  padding:4px 0px;
  text-align: right;
}
.form-container{
  padding:0px 80px;
}
</style>